﻿@using System.IO
@using System.Linq
@using System.Reflection
@using App_Code;
@using BlogEngine.Core
@using BlogEngine.Core.Web.Extensions
@{
    Page.Title = Resources.labels.extensions;
    Layout = "../_Layout.cshtml";

    Security.DemandUserHasRight(Rights.AccessAdminPages, true);
    if (!WebUtils.CheckIfPrimaryBlog(false)) { return; }

    var extName = Request.QueryString["id"];
    if (string.IsNullOrWhiteSpace(extName))
    {
        Response.Redirect("default.cshtml");
        return;
    }

    var ext = ExtensionManager.GetExtension(extName);
    if (ext == null)
    {
        Response.Redirect("default.cshtml");
        return;
    }

    var extensionFilename = ext.GetPathAndFilename(false);
    var canWrite = Utils.CanWrite(Href(Utils.ApplicationRelativeWebRoot + "App_Code/Extensions"));
    var fileExists = !string.IsNullOrWhiteSpace(extensionFilename) && File.Exists(extensionFilename);
    
    var editLink = "";   
    if (canWrite)
    {
        editLink = "<a class=\"extEdit\" onclick=\"return ShowEditor();\" href=\"#\">" + Resources.labels.edit + "</a>";
    }

    var val = string.Format("Source for [{0}] not found", extensionFilename);
    try
    {
        if (fileExists)
        {
            val = File.ReadAllText(extensionFilename);
        }
    }
    catch (Exception ex)
    {
        Utils.Log("admin/Extensions/Editor.cshtml", ex);
    }
}

<link rel="stylesheet" href="@Href(Utils.RelativeWebRoot + "Styles/syntaxhighlighter/shCore.css")" type="text/css" />
<link rel="stylesheet" href="@Href(Utils.RelativeWebRoot + "Styles/syntaxhighlighter/shThemeDefault.css")" type="text/css" />
<script src="@Href(Utils.RelativeWebRoot + "Scripts/syntaxhighlighter/shCore.js")" type="text/javascript"></script>
<script src="@Href(Utils.RelativeWebRoot + "Scripts/syntaxhighlighter/shBrushCSharp.js")" type="text/javascript"></script>
<script src="@Href(Utils.RelativeWebRoot + "Scripts/syntaxhighlighter/shBrushCss.js")" type="text/javascript"></script>
<script type="text/javascript">SyntaxHighlighter.all();</script>
<script type="text/javascript">
    function ShowEditor() {
        $('#extensionViewer').fadeOut();
        $('#extensionEditor').fadeIn();
        $('#saveArea').fadeIn();
        return false;
    }

    function CancelEdit() {
        CloseEditor();
        return false;
    }

    function BtnSaveClick() {
        var dto = { "sourceCode": $('#txtSourceCode').val(), "extensionName": $('#hdnExtensionName').val() };
        $.ajax({
            url: SiteVars.ApplicationRelativeWebRoot + "admin/AjaxHelper.aspx/UpdateExtensionSourceCode",
            data: JSON.stringify(dto),
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: onAjaxBeforeSend,
            success: function (result) {
                var rt = result.d;
                if (rt == true) {
                    ShowStatus("success", "Source code updated");
                    window.location.reload();
                }
                else {
                    ShowStatus("warning", "Error saving source code");
                }
            }
        });
        
        CloseEditor();
        return false;
    }

    function CloseEditor() {
        $('#extensionViewer').fadeIn();
        $('#extensionEditor').fadeOut();
        $('#saveArea').fadeOut();
    }
</script>

<div class="content-box-outer">

	<div class="content-box-full">
        <h1>@Resources.labels.sourceViewer: @extName @Html.Raw(editLink)</h1>
        <div id="extensionViewer">
<pre class="brush: c-sharp;">// @extensionFilename

@val</pre>

        </div>
        <div id="extensionEditor" style="display:none">
            <textarea id="txtSourceCode" style="width: 100%; min-height: 350px;">
@val
            </textarea>
        </div>
        <div id="saveArea" style="padding:5px 0 0 0; display: none">
            <input type="button" id="btnSave" class="btn primary" value="@Resources.labels.save" onclick="return BtnSaveClick()"  />
            @Resources.labels.or <a href="" onclick="return CancelEdit();">@Resources.labels.cancel</a>
        </div>
	</div>
    <input type="hidden" id="hdnExtensionName" value="@extName" />
</div>